package com.cmes.system.web.controller;

import com.cmes.core.Pagination;
import com.cmes.core.annotation.OperateLog;
import com.cmes.core.domain.bo.IdsBO;
import com.cmes.core.utils.BeanUtils;
import com.cmes.jdbc.customquery.CustomQueryBO;
import com.cmes.system.api.bo.DictBO;
import com.cmes.system.core.entity.DictEntity;
import com.cmes.system.core.entity.DictItemEntity;
import com.cmes.system.core.service.DictService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * 字典管理
 *
 * @author Bruce.Gong
 * @since 1.0.0-SNAPSHOT
 */
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/dict")
public class DictController {

    private final String MODULE = "字典管理";
    private final DictService dictService;

    @PostMapping("search")
    public Pagination<DictEntity> search(@RequestBody @Valid CustomQueryBO bo) {
        return dictService.page(bo);
    }

    @PostMapping("searchItems")
    public Pagination<DictItemEntity> listItems(@RequestBody @Valid CustomQueryBO bo) {
        return dictService.searchItems(bo);
    }

    @OperateLog(module = MODULE, businessType = "ADD")
    @PostMapping("add")
    public Long add(@RequestBody @Valid DictBO.AddBO bo) {
        DictEntity entity = BeanUtils.copyProperties(bo, new DictEntity());
        dictService.canAdd(entity);
        entity.setSys(false);
        dictService.insert(entity);
        return entity.getId();
    }

    @OperateLog(module = MODULE, businessType = "UPDATE")
    @PostMapping("update")
    public Long update(@RequestBody @Valid DictBO.UpdateBO bo) {
        DictEntity entity = BeanUtils.copyProperties(bo, new DictEntity());
        dictService.canUpdate(bo.getId());
        dictService.update(entity);
        return entity.getId();
    }

    @OperateLog(module = MODULE, businessType = "DELETE")
    @PostMapping("delete")
    public void delete(@RequestBody @Valid IdsBO bo) {
        List<DictEntity> dbs = dictService.canDelete(bo.getIds());
        dictService.delete(dbs);
    }

    @OperateLog(module = MODULE, businessType = "ADD_ITEM")
    @PostMapping("addItem")
    public Long addItem(@RequestBody @Valid DictBO.ItemAddBO bo) {
        DictItemEntity entity = BeanUtils.copyProperties(bo, new DictItemEntity());
        dictService.canAddItem(bo.getDictId(), entity);
        entity.setSort(entity.getSort() == null ? 0 : entity.getSort());
        dictService.insertItem(entity);
        return entity.getId();
    }

    @OperateLog(module = MODULE, businessType = "UPDATE_ITEM")
    @PostMapping("updateItem")
    public Long updateItem(@RequestBody @Valid DictBO.ItemUpdateBO bo) {
        DictItemEntity entity = BeanUtils.copyProperties(bo, new DictItemEntity());
        dictService.canUpdateItem(bo.getId());
        dictService.updateItem(entity);
        return entity.getId();
    }

    @OperateLog(module = MODULE, businessType = "DELETE_ITEM")
    @PostMapping("deleteItem")
    public void deleteItem(@RequestBody @Valid IdsBO bo) {
        dictService.canDeleteItem(bo.getIds());
        dictService.deleteItems(bo.getIds());
    }

}
